package com.beiming.moudle.auth.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.beiming.common.utils.Result;
import com.beiming.moudle.auth.entity.CategoryEntity;
import com.beiming.moudle.auth.entity.ProductImageEntity;
import com.beiming.moudle.auth.entity.ProductInfoEntity;
import com.beiming.moudle.auth.entity.WarehouseEntity;
import com.beiming.moudle.auth.entity.utillClass.ProInfoPage;
import com.beiming.moudle.auth.service.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 商品信息
 */
@RestController
@RequestMapping("/product")
public class ProInfoController {
    private final ProInfoService proInfoService;
    private final BussService bussService;
    private final ProductImageService productImageService;
    private final CategoryService categoryService;
    private final WarehouserService warehouserService;

    public ProInfoController(ProInfoService proInfoService, BussService bussService, ProductImageService productImageService, CategoryService categoryService, WarehouserService warehouserService) {
        this.bussService = bussService;
        this.proInfoService = proInfoService;
        this.productImageService = productImageService;
        this.categoryService = categoryService;
        this.warehouserService = warehouserService;
    }

    @PostMapping("/info")
    public Result getMsgPage(@RequestBody ProInfoPage pa) {
        if (bussService.getBussId(pa) != 0) {
            pa.setBussinessId(bussService.getBussId(pa));
            System.out.println("========" + bussService.getBussId(pa));
        }

        IPage<ProductInfoEntity> proPage = proInfoService.getMsgPage(pa);
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if (pa.getCurrentPage() > proPage.getPages()) {
            proPage = proInfoService.getMsgPage(pa);
        }
        List<ProductInfoEntity> productInfoEntities = proPage.getRecords();
        System.out.println(productInfoEntities.toArray());
        for (ProductInfoEntity productInfoEntity : productInfoEntities) {
            productInfoEntity.setBussinessName(bussService.getBussUser(productInfoEntity.getBussinessId()));
            productInfoEntity.setBussinessImgUrl(productImageService.getProductImage(productInfoEntity.getProductId()).getPicUrl());
            productInfoEntity.setOneCategory(categoryService.getCateEntity(productInfoEntity.getOneCategoryId()).getCategoryName());

            productInfoEntity.setTwoCategory(categoryService.getCateEntity(productInfoEntity.getTwoCategoryId()).getCategoryName());
            productInfoEntity.setCurrentCnt(warehouserService.getWareNum(productInfoEntity.getProductId()));
        }

        return Result.ok(proPage);
    }

    @PostMapping("/setStatus")
    private Result setStatus(@RequestBody ProductInfoEntity productInfoEntity) {
        int i = proInfoService.updateProStatus(productInfoEntity);
        return Result.ok(i);
    }

    @PostMapping("/update")
    private Result update(@RequestBody ProductInfoEntity productInfoEntity) {
        int i = proInfoService.updateProStatus(productInfoEntity);
        return Result.ok(i);
    }

    @PostMapping("/getOneCate")
    private Result getOneCate(@RequestBody CategoryEntity categoryEntity) {
        return Result.ok(categoryService.getCategory(0));
    }

    @PostMapping("/getTwoCate")
    private Result getTwoCate(@RequestBody CategoryEntity categoryEntity) {
        return Result.ok(categoryService.getCategory(categoryEntity.getParentId()));
    }

    @Value("${APP.path}")
    private String PhotoPath;

    @PostMapping("/upload")
    public Result upload(@RequestParam("photoFile") MultipartFile file) {
        System.out.println("===============================");
        System.out.println(PhotoPath);
        String originalFilename = file.getOriginalFilename();

//        根据最后一个.的索引来截取后缀
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName = UUID.randomUUID().toString() + substring;
        File dir = new File(PhotoPath);
        if (!dir.exists()) {
            dir.mkdir();
        }
        try {
            file.transferTo(new File(PhotoPath + fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(fileName);
        return Result.ok(fileName);
    }

    @PostMapping("/addProduct")
    private Result addProduct(@RequestBody ProductInfoEntity productInfoEntity) {
        proInfoService.addProduct(productInfoEntity);
        System.out.println("===========================");
        System.out.println(productInfoEntity);
        ProductImageEntity productImageEntity = new ProductImageEntity();
        productImageEntity.setPicUrl(productInfoEntity.getBussinessImgUrl());
        productImageEntity.setProductId(productInfoEntity.getProductId());
        productImageEntity.setPicDesc(productInfoEntity.getDescript());
        productImageService.setProductImage(productImageEntity);
        WarehouseEntity warehouseEntity = new WarehouseEntity();
        warehouseEntity.setProductId(productInfoEntity.getProductId());
        warehouseEntity.setCurrentCnt(0);
        warehouserService.addWare(warehouseEntity);
        return Result.ok();
    }

    @PostMapping("/updateWarehouse")
    private Result updateWarehouse(@RequestBody ProInfoPage proInfoPage) {
        System.out.println("+++++++++");
        System.out.println(proInfoPage);
        WarehouseEntity warehouseEntity = new WarehouseEntity();
        warehouseEntity.setProductId(proInfoPage.getProductId());
        warehouseEntity.setCurrentCnt(proInfoPage.getCurrentCnt());
        return Result.ok(warehouserService.setWareNum(warehouseEntity));
    }
    @PostMapping("/getProduct")
    public Result getPage(@RequestBody ProInfoPage pa) {
        System.out.println(pa.getCategoryId()+"++++++++++++++++");
        if (bussService.getBussId(pa) != 0) {
            pa.setBussinessId(bussService.getBussId(pa));
            System.out.println("========" + bussService.getBussId(pa));
        }
        IPage<ProductInfoEntity> proPage = proInfoService.getPage(pa);
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if (pa.getCurrentPage() > proPage.getPages()) {
            proPage = proInfoService.getPage(pa);
        }
        List<ProductInfoEntity> productInfoEntities = proPage.getRecords();
        System.out.println(productInfoEntities.toArray().toString());
        for (ProductInfoEntity productInfoEntity : productInfoEntities) {
            productInfoEntity.setBussinessName(bussService.getBussUser(productInfoEntity.getBussinessId()));
            productInfoEntity.setBussinessImgUrl(productImageService.getProductImage(productInfoEntity.getProductId()).getPicUrl());
            productInfoEntity.setOneCategory(categoryService.getCateEntity(productInfoEntity.getOneCategoryId()).getCategoryName());
            productInfoEntity.setTwoCategory(categoryService.getCateEntity(productInfoEntity.getTwoCategoryId()).getCategoryName());
            productInfoEntity.setCurrentCnt(warehouserService.getWareNum(productInfoEntity.getProductId()));
        }
        return Result.ok(proPage);
    }
    @PostMapping("getPro")
    public Result getPa(@RequestBody ProInfoPage pa) {
        System.out.println(pa+"===========");
        ProductInfoEntity productInfoEntity = proInfoService.getProduct(pa.getProductId());
        System.out.println(productInfoEntity);
        productInfoEntity.setBussinessImgUrl(productImageService.getProductImage(productInfoEntity.getProductId()).getPicUrl());
        productInfoEntity.setOneCategory(categoryService.getCateEntity(productInfoEntity.getOneCategoryId()).getCategoryName());
        productInfoEntity.setTwoCategory(categoryService.getCateEntity(productInfoEntity.getTwoCategoryId()).getCategoryName());
        productInfoEntity.setCurrentCnt(warehouserService.getWareNum(productInfoEntity.getProductId()));
        System.out.println(productInfoEntity+"===========");
        return  Result.ok(productInfoEntity);
    }
}
